use std::io;
use rand::Rng;

fn main() {
    println!("猜数字游戏！");
    
    // 生成 1 到 100 之间的随机数
    let secret_number = rand::thread_rng().gen_range(1..=100);
    
    loop {
        println!("请输入你的猜测：");
        
        let mut guess = String::new();
        
        // 读取用户输入
        io::stdin()
            .read_line(&mut guess)
            .expect("无法读取输入");
        
        // 将输入转换为整数
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("请输入一个有效的数字！");
                continue;
            }
        };
        
        println!("你猜的是：{}", guess);
        
        // 判断猜测结果
        match guess.cmp(&secret_number) {
            std::cmp::Ordering::Less => println!("太小了！"),
            std::cmp::Ordering::Greater => println!("太大了！"),
            std::cmp::Ordering::Equal => {
                println!("恭喜你，猜对了！");
                break;
            }
        }
    }
}
